package financial

import (
	"github.com/beego/beego/v2/client/orm"
	"ltbz/cultural_tourism_api/utils"
)

//故宫收入统计

type ImperialPalace struct {
}

// 故宫收入-财务数据
type ImperialPalaceFinancialData struct {
	CurrentDay   *FinancialDataItem `description:"当日数据"`
	CurrentWeek  *FinancialDataItem `description:"当周数据"`
	CurrentMonth *FinancialDataItem `description:"当月数据"`
	Total        *FinancialDataItem `description:"总计数据"`
}

type FinancialDataItem struct {
	//VipIncome            float64 `description:"普通会员收入(元)"`
	//SvipIncome           float64 `description:"超级会员收入(元)"`
	TradeOrderTotalMoney float64 `description:"订单总金额"` //总金额
	TradeOrderTotal      int     `description:"订单总数"`  //总订单数
}

type ImperialPalaceOrderListReq struct {
	TradeCode   string `description:"订单编码"`
	UserCode    string `description:"用户ID"`
	Mobile      string `description:"手机号"`
	GoodsCode   string `description:"商品编码"`
	GoodsName   string `description:"商品名称"`
	StartTime   string `description:"开始时间"`
	EndTime     string `description:"结束时间"`
	TradeStatus string `description:"订单状态:'待支付','支付中','支付失败','待使用','已使用','退款中','退款成功','退款失败','已生效','已取消'"`
	utils.PagingReq
}

type ImperialPalaceOrderView struct {
	TradeId        int     `description:"交易id"`
	TradeType      string  `description:"交易类型"`
	TradeCode      string  `description:"交易编码"`
	WxUserId       int     `description:"下单人id"`
	TradeMoney     float64 `description:"交易金额"`
	CouponMoney    float64 `description:"优惠金额"`
	TradeStatus    string  `description:"'待支付','支付中','支付失败','待使用','已使用','退款中','退款成功','退款失败','已生效'"`
	CreateTime     string  `description:"创建时间"`
	ModifyTime     string  `description:"修改时间"`
	CreateDate     string  `description:"创建日期"`
	PayTime        string  `description:"支付成功时间"`
	ExpirationDate string  `description:"过期日期"`
	GoodsType      int     `description:"商品类型:1:香,2:vip,3:门票"`
	GoodsId        int     `description:"商品Id"`
	Mobile         string  `description:"手机号"`
	GoodsCode      string  `description:"商品编码"`
	GoodsName      string  `description:"商品名称"`
	GoodsPrice     float64 `description:"商品价格"`
	BuyGoodNum     int     `description:"购买的商品数量"`
}

func (r *ImperialPalace) GetImperialPalaceOrderPageCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM trade AS a
            INNER JOIN wx_user AS b ON a.wx_user_id=b.wx_user_id AND b.is_virtual_user=0
            LEFT JOIN goods AS c ON a.goods_id=c.goods_id
            WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *ImperialPalace) GetImperialPalaceOrderPage(condition string, pars []interface{}, startSize, pageSize int) (items []*ImperialPalaceOrderView, err error) {
	o := orm.NewOrm()
	sql := `SELECT a.*,b.mobile,c.goods_code,c.goods_name,c.goods_price FROM trade AS a
            INNER JOIN wx_user AS b ON a.wx_user_id=b.wx_user_id AND b.is_virtual_user=0
            LEFT JOIN goods AS c ON a.goods_id=c.goods_id
     WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type ImperialPalaceOrderListPageResp struct {
	List   []*ImperialPalaceOrderView
	Paging *utils.PagingResp
}

type TradeAmountStatistics struct {
	TradeMoney  float64
	TradeStatus string `description:"交易状态:门票,香票用使用,VIP用生效，'待支付','支付中','支付失败','待使用','已使用','退款中','退款成功','退款失败','已生效','已取消'"`
}

//func (r *ImperialPalace) GetTradeAmountStatistics(condition string, pars []interface{}) (list []*TradeAmountStatistics, err error) {
//	o := orm.NewOrm()
//	sql := `SELECT SUM(trade_money) AS trade_money,trade_status FROM trade AS a
//            INNER JOIN wx_user AS b ON a.wx_user_id=b.wx_user_id AND b.is_virtual_user=0
//            WHERE 1=1 `
//	if condition != "" {
//		sql += condition
//	}
//	sql += ` GROUP BY a.trade_status `
//	_, err = o.Raw(sql, pars).QueryRows(&list)
//	return
//}

type ImperialPalaceOrderListExportReq struct {
	TradeCode   string `description:"订单编码"`
	UserCode    string `description:"用户ID"`
	Mobile      string `description:"手机号"`
	GoodsCode   string `description:"商品编码"`
	GoodsName   string `description:"商品名称"`
	StartTime   string `description:"开始时间"`
	EndTime     string `description:"结束时间"`
	TradeStatus string `description:"订单状态:'待支付','支付中','支付失败','待使用','已使用','退款中','退款成功','退款失败','已生效','已取消'"`
}

func (r *ImperialPalace) GetImperialPalaceOrderExportPage(condition string, pars []interface{}) (items []*ImperialPalaceOrderView, err error) {
	o := orm.NewOrm()
	sql := `SELECT a.*,b.mobile,c.goods_code,c.goods_name,c.goods_price FROM trade AS a
            INNER JOIN wx_user AS b ON a.wx_user_id=b.wx_user_id AND b.is_virtual_user=0
            LEFT JOIN goods AS c ON a.goods_id=c.goods_id
     WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type TradeOrderStatistics struct {
	TradeOrderTotalMoney float64 `description:"订单总金额"` //总金额
	TradeOrderTotal      int     `description:"订单总数"`  //总订单数
}

func (r *ImperialPalace) GetTradeOrderStatistics(condition string, pars []interface{}) (list *TradeOrderStatistics, err error) {
	o := orm.NewOrm()
	sql := `SELECT SUM(trade_money) AS trade_order_total_money,COUNT(1) AS trade_order_total FROM trade AS a 
           INNER JOIN wx_user AS b ON a.wx_user_id=b.wx_user_id AND b.is_virtual_user=0
           WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	//sql += ` GROUP BY a.trade_status `
	err = o.Raw(sql, pars).QueryRow(&list)
	return
}
